using UnityEngine;

namespace FutureCorePlugin
{
    public class TransformAdaptive : MonoBehaviour
    {
        private Transform tf;

        private void Awake()
        {
            tf = GetComponent<Transform>();
            Adaptive();
        }

#if UNITY_EDITOR
        private float currScreenHeight;
        private float currScreenWidth;

        private void Update()
        {
            if (currScreenHeight != Screen.height || currScreenWidth != Screen.width)
            {
                currScreenHeight = Screen.height;
                currScreenWidth = Screen.width;
                AdaptiveConst.CurrAspectRatio = (float)Screen.height / Screen.width;
                Adaptive();
            }
        }
#endif

        private void Adaptive()
        {
            if (tf == null) return;

            tf.localScale = Vector3.one;

            float currHeight = Screen.height;
            float currWidth = Screen.width;

            float standardAspectRatio = AdaptiveConst.StandardAspectRatio;
            float currAspectRatio = AdaptiveConst.CurrAspectRatio;
            float scale = 0f;

            if (currAspectRatio > standardAspectRatio)
            {
                scale = currAspectRatio / standardAspectRatio;
            }
            else
            {
                scale = standardAspectRatio / currAspectRatio;
            }

            tf.localScale *= scale;
        }
    }
}